package integracion;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import dominio.*;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Diana Uribe
 */
public class ControlInventario {

    private SqlMapClient sqlMap;

    public ControlInventario() throws IOException, SQLException {

        String resource = "configuracion/sql-map-config.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        this.sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
    }

    public boolean agregarInventario(Inventario inventario) {
        try {
            getSqlMap().insert("agregarInventario", inventario);
            return true;

        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public List<Inventario> consultarInventario(){

        try{

            List inventario = sqlMap.queryForList("consultarInventario");
            return inventario;

        } catch (SQLException e){
            Logger.getLogger(Inventario.class.getName()).log(Level.SEVERE,null,e);
            return null;
        }

    }

    public boolean eliminarInventario(Inventario inventario) {

        try {
            sqlMap.delete("eliminarInventario", inventario);
            return true;

        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public boolean modificarInventario(Inventario inventario) {

        try {
            sqlMap.update("modificarInventario",inventario);
            return true;

        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public SqlMapClient getSqlMap() {
        return sqlMap;
    }

    public void setSqlMap(SqlMapClient sqlMap) {
        this.sqlMap = sqlMap;
    }

}
